package com.stars.easyms.gateway.autoconfigure;

import com.stars.easyms.base.exception.EasyMsInitException;
import com.stars.easyms.gateway.filter.*;
import com.stars.easyms.gateway.properties.EasyMsGatewayProperties;
import com.stars.easyms.gateway.swagger.EasyMsGatewaySwaggerController;
import com.stars.easyms.gateway.swagger.EasyMsSwaggerWebFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * <p>className: EasyMsGatewayAutoConfiguration</p>
 * <p>description: EasyMs的网关自动配置类</p>
 *
 * @author guoguifang
 * @version 1.6.1
 * @date 2020/8/26 3:20 下午
 */
@Configuration
@EnableConfigurationProperties(EasyMsGatewayProperties.class)
public class EasyMsGatewayAutoConfiguration {

    private EasyMsGatewayProperties gatewayProperties;

    public EasyMsGatewayAutoConfiguration(EasyMsGatewayProperties easyMsGatewayProperties) {
        this.gatewayProperties = easyMsGatewayProperties;
    }

    @Bean
    public EasyMsAuthorizationFilter easyMsAuthorizationFilter() {
        return new EasyMsAuthorizationFilter();
    }

    @Bean
    public EasyMsRequestFilter easyMsRequestFilter() {
        return new EasyMsRequestFilter();
    }

    @Bean
    public EasyMsResponseFilter easyMsResponseTraceFilter() {
        return new EasyMsResponseFilter(gatewayProperties);
    }

    @Bean
    public EasyMsClearTraceFilter easyMsClearRequestTraceFilter() {
        return new EasyMsClearTraceFilter();
    }

    @Bean
    public EasyMsDebugFilter easyMsDebugFilter() {
        return new EasyMsDebugFilter();
    }

    @Bean
    public EasyMsSwaggerWebFilter easyMsSwaggerWebFilter() {
        return new EasyMsSwaggerWebFilter(gatewayProperties);
    }

    @Bean
    public EasyMsGatewaySwaggerController easyMsGatewaySwaggerController() {
        return new EasyMsGatewaySwaggerController(gatewayProperties);
    }

    @PostConstruct
    private void init() {
        // 对EasyMsGatewayProperties进行验证，如果需要加解密则必须要有秘钥和秘钥偏移量
        if (gatewayProperties.getEncrypt().isEnabled()) {
            StringBuilder stringBuilder = new StringBuilder();
            if (StringUtils.isBlank(gatewayProperties.getEncrypt().getSecret())) {
                stringBuilder.append("接口参数秘钥为空，请使用")
                        .append(EasyMsGatewayProperties.GATEWAY_PREFIX)
                        .append(".encrypt.secret进行配置!");
            }
            if (StringUtils.isBlank(gatewayProperties.getEncrypt().getIv())) {
                stringBuilder.append("接口参数秘钥偏移量为空，请使用")
                        .append(EasyMsGatewayProperties.GATEWAY_PREFIX)
                        .append(".encrypt.iv进行配置!");
            }
            String requestKey = gatewayProperties.getEncrypt().getRequestKey();
            String responseKey = gatewayProperties.getEncrypt().getResponseKey();
            if (StringUtils.isBlank(requestKey) && StringUtils.isBlank(responseKey)) {
                stringBuilder.append("接口加解密时的requestKey或responseKey为空，请使用")
                        .append(EasyMsGatewayProperties.GATEWAY_PREFIX)
                        .append(".encrypt.requestKey或responseKey进行配置!");
            }
            String errorMessage = stringBuilder.toString();
            if (!StringUtils.isBlank(errorMessage)) {
                throw new EasyMsInitException(errorMessage);
            }

            // 如果校验通过，则判断requestKey与responseKey是否有效，如果无效则设置成相应的
            if (StringUtils.isBlank(requestKey)) {
                gatewayProperties.getEncrypt().setRequestKey(responseKey);
            }
            if (StringUtils.isBlank(responseKey)) {
                gatewayProperties.getEncrypt().setResponseKey(requestKey);
            }
        }
    }

}
